import {
  SAVE_IS_BELL,
  SAVE_MACHINE_STATUS,
  SAVE_MELT_DATA, SAVE_OPEN_BELL,
  SAVE_PAUSE_CACHE_TIME,
  SAVE_PCR_DATA,
  SAVE_REMAIN_TIME,
  SAVE_RUN_EXPERIMENT_NAME,
  SAVE_RUN_PERCENT,
  SAVE_RUN_START,
  SAVE_RUN_TIME,
  SAVE_START_TIME,
  SAVE_START_TIME_STAMP,
  SAVE_STOP_TIME,
  SAVE_TEMP_DATA,
  SAVE_CURRENT_PERENT
} from "./constants";
import {calcTime} from "../../../../common/utils";
import storageSession from "redux-persist/lib/storage/session";
import {persistReducer} from "redux-persist";

const persistConfig = {
  key: "user",
  storage: storageSession,
  whitelist: ["runStart"] //  白名单:runStart会存入缓存，其他不会存
};

const defaultState = {
  runStart: false,
  currentTempData: [],
  machineStatus: {},
  pcrData: {
    channel1: [],
    channel2: [],
    channel3: [],
    channel4: [],
    channel5: [],
    channel6: []
  },
  meltData: [],
  startTime: "",
  stopTime: "",
  runTime: calcTime(0),
  remainTime: calcTime(0),
  runPercent: 0,
  isBell: false,
  runExperimentName: "",
  startTimeStamp: 0,
  pauseCacheTime: 0,
  openBell: false,
  currentPercent:0
};

function reducer(state = defaultState, action) {
  switch (action.type) {
    case SAVE_RUN_START :
      return {...state, runStart: action.runStart};
    case SAVE_TEMP_DATA :
      return {...state, currentTempData: action.currentTempData};
    case SAVE_MACHINE_STATUS :
      return {...state, machineStatus: action.machineStatus};
    case SAVE_PCR_DATA :
      return {...state, pcrData: action.pcrData};
    case SAVE_MELT_DATA :
      return {...state, meltData: action.meltData};
    case SAVE_START_TIME :
      return {...state, startTime: action.startTime};
    case SAVE_STOP_TIME :
      return {...state, stopTime: action.stopTime};
    case SAVE_RUN_TIME :
      return {...state, runTime: action.runTime};
    case SAVE_REMAIN_TIME :
      return {...state, remainTime: action.remainTime};
    case SAVE_RUN_PERCENT :
      return {...state, runPercent: action.runPercent};
    case SAVE_IS_BELL :
      return {...state, isBell: action.isBell};
    case SAVE_RUN_EXPERIMENT_NAME :
      return {...state, runExperimentName: action.runExperimentName};
    case SAVE_START_TIME_STAMP :
      return {...state, startTimeStamp: action.startTimeStamp};
    case SAVE_PAUSE_CACHE_TIME :
      return {...state, pauseCacheTime: action.pauseCacheTime};
      case SAVE_OPEN_BELL :
      return {...state, openBell: action.openBell};
      case SAVE_CURRENT_PERENT :
      return {...state, currentPercent: action.currentPercent};
    default:
      return state;
  }
}

export default persistReducer(persistConfig, reducer);
